<?php

namespace App\Models\Api;

use App\Models\Common\CollOption;

class ResumeEducationExp extends ApiModel
{
    //表名
    protected $table = 'resume_education_exp';

    const CREATED_AT = 'addtime';
    const UPDATED_AT = 'updatetime';

    protected $guarded = [];

    /**
     * 追加到模型数组表单的访问器,需要时自行使用append(),查询结束后使用
     *
     * @var array
     */
    protected $appends = ['education_name', 'starttime_name', 'endtime_name'];

    /**
     * The attributes that should be hidden for arrays.
     * 隐藏字段
     * @var array
     */
    protected $hidden = [
        'addtime', 'updatetime', 'uid',
        'resume_id'
    ];

    /**
     * 要触发的所有关联关系(子类增删改更新父类updatetime)
     *
     * @var array
     */
    protected $touches = ['resume'];

    /**
     * 技能所属简历
     */
    public function resume()
    {
        return $this->belongsTo(Resume::class, 'resume_id', 'id');
    }

    public function getEducationNameAttribute()
    {
        return CollOption::getOptionName($this->attributes['education'], 'education');
    }

    public function getStarttimeNameAttribute()
    {
        $value = $this->attributes['starttime'];
        return $value == '0000-00-00' ? '至今' : substr($value, 0, 7);
    }

    public function getEndtimeNameAttribute()
    {
        $value = $this->attributes['endtime'];
        return $value == '0000-00-00' ? '至今' : substr($value, 0, 7);
    }

    public function getDescriptionAttribute($value)
    {
        return $value ?: '';
    }
}
